package com.ruoyi.system.basicInformation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.system.basicInformation.vo.TAlarmCountVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.ruoyi.system.basicInformation.bo.TAlarmAddBo;
import com.ruoyi.system.basicInformation.bo.TAlarmQueryBo;
import com.ruoyi.system.basicInformation.bo.TAlarmEditBo;
import com.ruoyi.system.basicInformation.domain.TAlarm;
import com.ruoyi.system.basicInformation.mapper.TAlarmMapper;
import com.ruoyi.system.basicInformation.vo.TAlarmVo;
import com.ruoyi.system.basicInformation.service.ITAlarmService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 报警信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-05-19
 */
@Service
public class TAlarmServiceImpl extends ServiceImpl<TAlarmMapper, TAlarm> implements ITAlarmService {

    @Override
    public TAlarmVo queryById(Long id) {
        TAlarm db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, TAlarmVo.class);
    }

    @Override
    public List<TAlarmVo> queryList(TAlarmQueryBo bo) {
        LambdaQueryWrapper<TAlarm> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getTerminalType()), TAlarm::getTerminalType, bo.getTerminalType());
        lqw.eq(StrUtil.isNotBlank(bo.getStateType()), TAlarm::getStateType, bo.getStateType());
        lqw.like(StrUtil.isNotBlank(bo.getContent()), TAlarm::getContent, bo.getContent());
        if (bo.getStartDateTime() != null && bo.getEndDateTime() != null)
            lqw.between(TAlarm::getDateTime, bo.getStartDateTime(), bo.getEndDateTime());
        lqw.orderByDesc(TAlarm::getDateTime);


        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<TAlarmVo> queryListForExcel(TAlarmQueryBo bo) {
        return this.baseMapper.listTAlarmListDTO(bo);
    }

    @Override
    public List<TAlarmVo> queryListForMap(TAlarmQueryBo bo) {
        return this.baseMapper.queryListForMap(bo);
    }

    @Override
    public List<TAlarmVo> listTAlarmVo(TAlarmQueryBo bo) {
        return this.baseMapper.listTAlarmVo(bo);
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<TAlarmVo> entity2Vo(Collection<TAlarm> collection) {
        List<TAlarmVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TAlarmVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TAlarm> page = (Page<TAlarm>) collection;
            Page<TAlarmVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TAlarmAddBo bo) {
        TAlarm add = BeanUtil.toBean(bo, TAlarm.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean insertByTerminalAddBo(TAlarmAddBo bo) {
        return this.baseMapper.insertByTerminalAddBo(bo) > 0;
    }

    @Override
    public Boolean updateByEditBo(TAlarmEditBo bo) {
        TAlarm update = BeanUtil.toBean(bo, TAlarm.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    @Override
    public Boolean apply(TAlarmEditBo bo) {
        return this.baseMapper.apply(bo.getId(), bo.getVerify()) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TAlarm entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }

    public TAlarmCountVo toDayCount(Integer isToDay) {
        return this.baseMapper.toDayCount(isToDay);
    }
}
